package com.itheima.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.*;

@Service(interfaceClass =OrderSettingService.class )
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
  @Autowired
  private OrderSettingDao orderSettingDao;
    @Override
    public void upload(List<OrderSetting> orderSettings) {
      if (orderSettings!=null && orderSettings.size()>0) {
        for (OrderSetting orderSetting : orderSettings) {
          Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
          if(count>0){
            orderSettingDao.update(orderSetting);
          }else {
            orderSettingDao.insert(orderSetting);
          }
        }
      }
    }

  @Override
  public List<Map> getOrderSettingByMonth(String date) {
    String start = date + "-1";
    String end = date+"-31";
    List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(start, end);
    List<Map>getOrderSettingByMonth = new ArrayList<>();
    for (OrderSetting orderSetting : list) {
      Map<String,Object> map = new HashMap<>();
      int number = orderSetting.getNumber();
      int reservations = orderSetting.getReservations();
      int day = orderSetting.getOrderDate().getDate();
       map.put("date",day);
       map.put("number",number);
       map.put("reservations",reservations);
       getOrderSettingByMonth.add(map);
    }
    return getOrderSettingByMonth;
  }

  @Override
  public void editNumberByDate(OrderSetting orderSetting) {
    Date orderDate = orderSetting.getOrderDate();
    Integer count = orderSettingDao.findCountByOrderDate(orderDate);
    if(count>0){
      orderSettingDao.update(orderSetting);
    }else {
      orderSettingDao.insert(orderSetting);
    }
  }
}
